package czk.concrent.threadSafeCollection.copyOnWriteArray;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * CopyOnWriteArrayList迭代器的弱一致性
 * @Author:ChenZhangKun
 * @Date: 2020/7/28 23:24
 */
public class CopyOnWriteArrayDemo {
    public static void main(String[] args) throws InterruptedException {
        CopyOnWriteArrayList<Integer> copyOnWriteArrayList=new CopyOnWriteArrayList<>();
        copyOnWriteArrayList.add(1);
        copyOnWriteArrayList.add(2);
        copyOnWriteArrayList.add(3);
        copyOnWriteArrayList.add(4);
        Iterator<Integer> iterator = copyOnWriteArrayList.iterator();
        new Thread(()->{
            copyOnWriteArrayList.remove(0);
            System.out.println(copyOnWriteArrayList);// 2,3,4
        }).start();
        Thread.sleep(1*1000);
        while (iterator.hasNext()){
            System.out.print(iterator.next()+" ");// 1,2,3,4
        }
    }
}
